package cn.tedu.carehome.foundation.controller;


import cn.tedu.carehome.foundation.ex.ServiceException;
import cn.tedu.carehome.foundation.pojo.dto.FoodUpdateDTO;
import cn.tedu.carehome.foundation.pojo.vo.FoodAdminVO;
import cn.tedu.carehome.foundation.pojo.vo.FoodStandardVO;
import cn.tedu.carehome.foundation.pojo.vo.FoodUpdateVO;
import cn.tedu.carehome.foundation.pojo.dto.FoodAddNewDTO;
import cn.tedu.carehome.foundation.restful.JsonPage;
import cn.tedu.carehome.foundation.restful.WebConsts;
import cn.tedu.carehome.foundation.service.IFoodService;
import cn.tedu.carehome.foundation.web.JsonResult;
import cn.tedu.carehome.foundation.web.ServiceCode;
import cn.tedu.carehome.foundation.service.IFoodService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/foods")

@Api(tags = "0.餐品管理模块")
public class FoodController {

    @Autowired
    private IFoodService foodService;

    @GetMapping("/list-by-page")
    @ApiOperation("带分页信息的餐品信息")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "页码",name = "page",example = "1"),
            @ApiImplicitParam(value = "每页条数",name="pageSize",example = "2")
    })
    public JsonResult<JsonPage<FoodStandardVO>> listFoodByPage(
            // 控制器方法参数可以设置默认值,在调用时如果这个参数没有值,会使用默认值
            // @RequestParam注解来实现默认值,WebConsts是当前项目中包含常量的类
            @RequestParam(required = false,defaultValue = WebConsts.DEFAULT_PAGE)
                    Integer page,
            @RequestParam(required = false,defaultValue = WebConsts.DEFAULT_PAGE_SIZE)
                    Integer pageSize
    ){
        JsonPage<FoodStandardVO> jsonPage=
                foodService.listFoods(page,pageSize);
        log.info("产品列表：{}",jsonPage);
        return JsonResult.ok(jsonPage);
    }//







    @PostMapping("/add-new")
    @ApiOperation("添加餐品")
    @PreAuthorize("hasAuthority('food/add-new')")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@Valid FoodAddNewDTO foodAddNewDTO){
        log.debug("开始处理添加餐品请求,参数:{}",foodAddNewDTO);
        foodService.addNew(foodAddNewDTO);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据id删除餐品")
    @PreAuthorize("hasAuthority('food/delete')")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "餐品ID", required = true, example = "9527", dataType = "long")
    public JsonResult<Void> deleteById(@Range(min = 1, max = 10000, message = "删除餐品失败，餐品ID非法！")
                         @PathVariable Long id) {
        log.debug("开始【模拟】处理【根据id删除餐品】的请求，参数：{}", id);
        foodService.deleteById(id);
        return JsonResult.ok();
    }


    @PostMapping("/update")
    @ApiOperation("修改餐品")
    @PreAuthorize("hasAuthority('food/update')")
    @ApiOperationSupport(order = 300)
    public JsonResult<Void> update(@Valid FoodUpdateDTO foodUpdateDTO) {
        log.debug("开始处理修改餐品请求,参数:{}",foodUpdateDTO);
        foodService.update(foodUpdateDTO);
        return JsonResult.ok();
    }

    @GetMapping("/{id:[0-9]+}")
    @ApiOperation(value="根据id请求餐品信息")
    @PreAuthorize("hasAuthority('food/update')")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id", value = "餐品ID", required = true, example = "9527", dataType = "long")
    public JsonResult<FoodStandardVO> selectById(@Range(min = 1, max = 100000, message = "查询餐品失败，餐品ID非法！")
                                         @PathVariable Long id){
        log.debug("开始处理[根据id查询餐品业务],参数:{}",id);
        FoodStandardVO foodStandardVO=foodService.getStandardById(id);
        return JsonResult.ok(foodStandardVO);
    }


    @GetMapping("/list")
    @ApiOperation(value="请求餐品列表")
    @ApiOperationSupport(order = 200)
    public  JsonResult<List<FoodAdminVO>> select(){
        log.debug("开始处理后台请求餐品列表");
        List<FoodAdminVO> list=foodService.queryFoodList();
        return JsonResult.ok(list);
    }
}
